package com.wwinsoft.framework.web.security;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.Lists;
import com.wwinsoft.framework.dao.HibernateUtils;
import com.wwinsoft.framework.entity.security.Authority;
import com.wwinsoft.framework.entity.security.Group;
import com.wwinsoft.framework.entity.security.Resource;
import com.wwinsoft.framework.pojo.common.JsNode;
import com.wwinsoft.framework.pojo.common.MenuTree;
import com.wwinsoft.framework.service.security.AuthorityService;
import com.wwinsoft.framework.utils.JsNodeHelper;
import com.wwinsoft.framework.utils.MenuUtils;
import com.wwinsoft.framework.web.CrudActionSupport;
import com.wwinsoft.modules.utils.encode.JsonBinder;
import com.wwinsoft.modules.utils.web.struts2.Struts2Utils;

@Namespace("/security")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "authority.action", type = "redirect") })
public class AuthorityAction extends CrudActionSupport<Authority> {

    private static final long serialVersionUID = -4052047494894591406L;

    @Autowired
    private AuthorityService authorityService;

    @Override
    public String execute() throws Exception {
        return super.execute();
    }

    private Long id;
    private Authority entity;

    private JsonBinder jsonBinder = JsonBinder.buildNormalBinder();

    public Authority getModel() {
        return entity;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = authorityService.getAuthority(id);
        } else {
            entity = new Authority();
        }
    }

    //-- CRUD Action 函数 --//
    @Override
    public String list() throws Exception {
        //        allAuthorityList = authorityService.getAllResouce();
        return SUCCESS;
    }

    @Override
    public String input() throws Exception {
        return INPUT;
    }

    @Override
    public String save() throws Exception {
        authorityService.saveAuthority(entity);
        addActionMessage("保存权限成功");
        return RELOAD;
    }

    @Override
    public String delete() throws Exception {
        authorityService.deleteAuthority(id);
        addActionMessage("删除权限成功");
        return RELOAD;
    }

    public void updateAuthority() throws Exception {
        JsNode<Long>  jsNode = JsNodeHelper.prepareJsNode(Long.class);
        Long returnId = 0L;
        if (!Long.valueOf(-1).equals(jsNode.getId())) {
            returnId = authorityService.updateNode(jsNode);
        }
        StringBuilder returnJson = new StringBuilder();
        returnJson.append("{\"status\":1,\"id\":").append(returnId).append("}");
        Struts2Utils.renderJson(returnJson.toString());
    }

    public void getResource(){
        String resourceId = Struts2Utils.getParameter("resourceId");
        if(StringUtils.isNotEmpty(resourceId)){
            Resource resource = authorityService.getResource(Long.valueOf(resourceId));
            Struts2Utils.renderJson(resource);
        }
    }

    public void saveResource(){
        String authorityId = Struts2Utils.getParameter("authorityId");
        String resourceId = Struts2Utils.getParameter("resourceId");
        String title = Struts2Utils.getParameter("title");
        String resourceType = Struts2Utils.getParameter("resourceType");
        String url = Struts2Utils.getParameter("url");
        String limitSql = Struts2Utils.getParameter("limitSql");
        Authority authority = authorityService.getAuthority(Long.valueOf(authorityId));

        Resource resource;
        if(StringUtils.isNotEmpty(resourceId)){
            resource = authorityService.getResource(Long.valueOf(resourceId));
        }else{
            resource = new Resource();
        }
        resource.setPosition(authority.getPosition());
        resource.setTitle(title);
        resource.setResourceType(resourceType);
        if(Resource.RESOURCE_TYPE_URL.equals(resourceType)){
             resource.setUrl(url);
        }else if(Resource.RESOURCE_TYPE_SQL.equals(resourceType)){
            resource.setLimitSql(limitSql);
        }
        List<Long>  authorityIds= Lists.newArrayList(Long.valueOf(authorityId));
        HibernateUtils.mergeByCheckedIds(resource.getAuthorityList(),authorityIds , Authority.class);

        authorityService.saveResource(resource);
        Struts2Utils.renderJson(resource);
    }

    public String getAuthorityJson() {
        List<Authority> allAuthoritys = authorityService.getAllAuthoritys();
        MenuTree menuTree = MenuUtils.buildMenuTreeWithRoot(allAuthoritys, "主菜单");
        return jsonBinder.toJson(menuTree);
    }

}